package com.article.service.impl;

import com.article.dao.AdminInfoDao;
import com.article.dto.ReturnError;
import com.article.enums.ReturnInfoEnum;
import com.article.model.AdminInfo;
import com.article.service.AdminInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/2.
 */
@Service("adminInfoService")
public class AdminInfoServiceImpl implements AdminInfoService {
    @Autowired
    private AdminInfoDao adminInfoDao;

    public Long countTotal(Map<String, Object> map) {
        return adminInfoDao.countTotal(map);
    }

    public PageInfo<AdminInfo> listByPage(Integer pageIndex, Integer limit, Map<String, Object> map) {
        PageHelper.startPage(pageIndex, limit);
        List<AdminInfo> adminInfoList = adminInfoDao.listByPage(map);
        return new PageInfo<AdminInfo>(adminInfoList);
    }

    public List<AdminInfo> listByMap(Map<String, Object> map) {
        return adminInfoDao.listByMap(map);
    }

    public AdminInfo getById(Long id) {
        return adminInfoDao.getById(id);
    }

    public AdminInfo getByMap(Map<String, Object> map) {
        return adminInfoDao.getByMap(map);
    }

    @Transactional
    public int updateLogin(AdminInfo adminInfo) {
        try {
            return adminInfoDao.update(adminInfo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Transactional
    public ReturnError saveAdmin(AdminInfo adminInfo) {
        try {
            int result = adminInfoDao.save(adminInfo);
            if (result != 0) {
                return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), ReturnInfoEnum.SUCCESS.getInfo(), null);
            }
            return new ReturnError(true, ReturnInfoEnum.FAIL.getType(), "已存在相同账号", null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Transactional
    public ReturnError updateAdmin(AdminInfo adminInfo) {
        try {
            int result = adminInfoDao.update(adminInfo);
            if (result != 0) {
                return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), ReturnInfoEnum.SUCCESS.getInfo(), null);
            }
            return new ReturnError(true, ReturnInfoEnum.FAIL.getType(), "已存在相同账号", null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Transactional
    public ReturnError removeAdmin(String ids) {
        String[] idarr = ids.split(",");
        try {
            int c = 0;
            for (String id : idarr) {
                int i = adminInfoDao.remove(Long.parseLong(id));
            }
            return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
